/*
 * ThreadLocalPtr.cpp
 *
 *	Author: stravant
 *	Description:
 *		
 */

#include "Include.h"
#include "ThreadLocalPtr.h"

namespace priv {

#if defined(OS_POSIX)

void ThreadPlatform::AllocSlot(SlotType& slot) {
	int err = pthread_key_create(&slot, nullptr);
	assert(err == 0);
}

void ThreadPlatform::FreeSlot(SlotType& slot) {
	int err = pthread_key_delete(slot);
	assert(err == 0);
}

void ThreadPlatform::SetSlotValue(SlotType& slot, void* value) {
	int err = pthread_setspecific(slot, value);
	assert(err == 0);
}

void* ThreadPlatform::GetSlotValue(SlotType& slot) {
	return pthread_getspecific(slot);
}

#elif defined(OS_WIN)

#include <windows.h>

void ThreadPlatform::AllocSlot(SlotType& slot) {
	slot = TlsAlloc();
	assert(slot != TLS_OUT_OF_INDEXES);
}

void ThreadPlatform::FreeSlot(SlotType& slot) {
	if (!TlsFree(slot))
		unreachable;
}

void ThreadPlatform::SetSlotValue(SlotType& slot, void* value) {
	TlsSetValue(slot, value);
}

void* ThreadPlatform::GetSlotValue(SlotType& slot) {
	return TlsGetValue(slot);
}

#endif

}
